<?php
/**
 * Navigation
 *
 * Singleton de navigation
 *
 * @author Mathieu
 */
class Navigation extends Object {
    protected $mainMenu;                //tableau contenant le menu principal
    protected $currentPage;             // Page actuelle

    protected static $navigation;       // Instance de la navigation

    function __construct() {
        
    }

    static function getInstance() {
        if(!isset(self::$navigation)) {
            self::$navigation = new Navigation();
        }
        return self::$navigation;
    }

    public function getMainMenu() {
        $db = Database::getInstance();
        if(!isset($this->mainMenu)) {
            $categories = Category::getAllCategory();
            $result = $db->query("SELECT sf.nom as name_lvl1, sf.slug as slug_lvl1, sf.id as id_lvl1, f.id as id_famille
                FROM sous_famille sf
                INNER JOIN famille f ON sf.id_famille = f.id
                INNER JOIN sous_sous_famille ssf ON sf.id = ssf.id_sous_famille
                INNER JOIN produits p ON p.famille_id = ssf.id
                GROUP BY sf.id
                ORDER BY sf.ordre");
             $record = $db->query("SELECT ssf.id as id_lvl2, ssf.nom as name_lvl2, ssf.slug as slug_lvl2,id_sous_famille
                    from sous_sous_famille ssf
                    INNER JOIN produits p ON p.famille_id = ssf.id
                    GROUP BY ssf.id");
            foreach($categories as $category){

                foreach($result as $data) {
                   if($category['id']==$data['id_famille']) {                      
                            foreach($record as $data2) {
                                if($data['id_lvl1']==$data2['id_sous_famille']){
                                    $lev2[]= array('subsubcategory'=>new SubSubCategory($data2['id_lvl2'],array('nom'=>$data2['name_lvl2'], 'slug'=>$data2['slug_lvl2'])));//array(ucfirst($data2['name_lvl2']), $data2['id_lvl2'],$data2['slug_lvl2'] );
                                }
                            }
                           if(isset($lev2)) {
                                $lev1[] = array('subcategory'=>new SubCategory($data['id_lvl1'],array('nom'=>$data['name_lvl1'], 'slug'=>$data['slug_lvl1'])), 'lvl2'=>$lev2); //array('lvl1'=>ucfirst($data['name_lvl1']), 'id_lvl1'=>$data['id_lvl1'], 'slug_lvl1'=>$data['slug_lvl1'], 'lvl2'=>$lev2);
                                unset($lev2);
                           } else
                              $lev1[] = array('subcategory'=>new SubCategory($data['id_lvl1'],array('nom'=>$data['name_lvl1'], 'slug'=>$data['slug_lvl1']))); //array('lvl1'=>ucfirst($data['name_lvl1']), 'id_lvl1'=>$data['id_lvl1'], 'slug_lvl1'=>$data['slug_lvl1']);
                   }
                }
               $menu[] = array('category'=>new Category($category['id'],$category), 'lvl1'=>$lev1); //array('id_categorie'=>ucfirst($category['id']), 'nom_categorie'=>ucfirst($category['nom']),'slug_categorie'=>ucfirst($category['slug']), 'lvl1'=>$lev1);
               unset($lev1);
            }
            $this->mainMenu = $menu;

        }
        return $this->mainMenu;
    }

    public function getCategoryUrl($id){
        if(!isset($this->mainMenu)) $this->getMainMenu();
        foreach($this->mainMenu as $category) {
            $cat = $category['category'];
            if($cat['id']==$id) $url=URL_PATH.'boutique/'.$cat['slug'].'/';
        }
        return (isset($url)) ? $url : null;
    }

    public function getSubCategoryUrl($id){
        if(!isset($this->mainMenu)) $this->getMainMenu();
        foreach($this->mainMenu as $category) {
            foreach($category['lvl1'] as $lvl1){

                $level1 = $lvl1['subcategory'];
                if($level1['id']==$id){
                    $cat = $category['category'];
                    $url=$this->getCategoryUrl($cat['id']).$level1['slug'].'/';
                }
            }
        }
        return (isset($url)) ? $url : null;
    }

    public function getSubSubCategoryUrl($id){
        if(!isset($this->mainMenu)) $this->getMainMenu();
        foreach($this->mainMenu as $category) {
            foreach($category['lvl1'] as $lvl1){
                foreach($lvl1['lvl2'] as $lvl2){
                    $level2 = $lvl2['subsubcategory'];
                    if($level2['id']==$id){
                        $cat = $category['category'];
                        $level1 = $lvl1['subcategory'];
                        $url=$this->getSubCategoryUrl($level1['id']).$level2['slug'].'/';
                    }
                }
            }
        }
        return (isset($url)) ? $url : null;
    }

    public function getRewrite($string) {
        $url = preg_replace("`\[.*\]`U","",$string);
        $url = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$url);
        $url = htmlentities($url, ENT_NOQUOTES, 'ISO-8859-1');
        $url = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i","\\1", $url );
        $url = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $url);
        $url = strtolower(trim($url, '-'));
        return (isset($url)) ? $url : null;
    }

    public function getCurrentPage() {
        $table = split("/", $_SERVER['SCRIPT_FILENAME']);
        $file = $table[sizeof($table)-1];
        $this->currentPage = substr($file, 0, strlen($file)-4);
        return $this->currentPage;
    }

    public function getCss() {
        if(!isset($this->currentPage)) $this->getCurrentPage();
        if(file_exists(SERVER_PATH."include/css/".$this->currentPage.".css"))
            return true;
        return false;
    }

    public function getJs() {
        if(!isset($this->currentPage)) $this->getCurrentPage();
        if(file_exists(SERVER_PATH."include/js/".$this->currentPage.".js"))
            return true;
        return false;
    }

    public function getCurrentUrl() {
        return "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    }

    public function getShortUrl() {
        $tinyurl = @file_get_contents("http://tinyurl.com/api-create.php?url=".$this->getCurrentUrl());
        if (!$tinyurl) {
            return $url;
        }
        return $tinyurl;
    }
    
   
}
?>
